package view;

import Dao.RalationalDataDao;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Vector;

public class UpdateRltFra extends JFrame {
    Connection conn;
    RalationalDataDao ralationalDataDao=new RalationalDataDao();
    String tableName;

    RelationFra rf;

    ArrayList<String> columnName=new ArrayList<>();
    ArrayList<String> columnType=new ArrayList<>();

    ArrayList<String> data=new ArrayList<>();

    JButton cancelBtn,OKBtn;

    JTextField[] textField;
    UpdateRltFra(Connection conn,RelationFra rf,String tableName,ArrayList<String> columnName,ArrayList<String> columnType,ArrayList<String> data){
        this.conn=conn;
        this.tableName=tableName;
        this.columnName=columnName;
        this.data=data;
        this.columnType=columnType;
        this.rf=rf;
        //初始化窗口
        setBounds(300,100,400,400);
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        getContentPane().setLayout(new GridLayout(columnName.size()+1,2));
        addContent();
        setVisible(true);
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        //监听
        addListener();
    }

    private void addListener() {
        //取消
        cancelBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                dispose();
            }
        });

        //确认修改
        OKBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                UpdataActPerformed();
                rf.fillTable();
            }
        });
    }

    /**
     * 修改函数
     */
    private void UpdataActPerformed() {
        ArrayList<String> newData=new ArrayList<>();
        //获取输入
        for (int i = 0; i < textField.length; i++) {
            newData.add(textField[i].getText());
        }
        try {
            //修改
            int affNum=ralationalDataDao.Updata(conn,tableName,columnName,columnType,newData);
            if(affNum>0){
                JOptionPane.showMessageDialog(null,"修改成功");
            }
            else {
                JOptionPane.showMessageDialog(null,"修改失败");
            }
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null,"修改失败");
            throw new RuntimeException(e);
        }
    }

    //添加窗口控件
    private void addContent() {
        int index=0;//不可修改geom
        for (int i = 0; i < columnName.size(); i++) {
            if(columnName.get(i).equalsIgnoreCase("geom"))
                index=i;
        }
        data.remove(index);
        columnName.remove(index);
        columnType.remove(index);

        //添加输入框
        textField=new JTextField[columnName.size()];
        for (int i = 0; i < columnName.size(); i++) {
            JLabel label = new JLabel(columnName.get(i));
            label.setPreferredSize(new Dimension(100,50));
            textField[i]=new JTextField(data.get(i));
            textField[i].setPreferredSize(new Dimension(200,50));
            if(columnName.get(i).equalsIgnoreCase("id")){
                textField[i].setEditable(false);
            }
            getContentPane().add(label);
            getContentPane().add(textField[i]);
        }
        //按钮
        cancelBtn=new JButton("取消");
        OKBtn=new JButton("修改");
        cancelBtn.setPreferredSize(new Dimension(50,20));
        OKBtn.setPreferredSize(new Dimension(50,20));
        getContentPane().add(OKBtn);
        getContentPane().add(cancelBtn);
    }

}
